package com.zzyl.controller.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.entity.Bed;
import com.zzyl.entity.Room;
import com.zzyl.exception.BusinessException;
import com.zzyl.service.BedService;
import com.zzyl.service.RoomService;
import com.zzyl.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class RoomController extends BaseController {
    @Autowired
    private RoomService roomService;
    @Autowired
    private BedService bedService;

    // 新增房间
    @PostMapping("/room/add")
    public ResponseResult save(@RequestBody Room room){
        room.setId(null);
        roomService.saveOrUpdateRoom(room);
        return success();
    }
    // 查询指定楼层下的房间
    @GetMapping("/room/getRoomsByFloorId/{floorId}")
    public ResponseResult findByFloorId(@PathVariable Long floorId){
    List<RoomVo> roomVoList  = roomService.findByFloorId(floorId);
        roomVoList.forEach(System.out::println);
        return success(roomVoList);
    }
    @GetMapping("/room/get/{id}")
    public ResponseResult getRoomById(@PathVariable Long id){
        RoomVo room = roomService.getRoomById(id);
        return success(room);
    }
    @PutMapping("/room/update")
    public ResponseResult updateRoom(@RequestBody Room room ){
        roomService.saveOrUpdateRoom(room);
        return success();
    }
    @DeleteMapping("/room/delete/{id}")
    public ResponseResult deleteById(@PathVariable Long id){
        LambdaQueryWrapper<Bed> bedLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bedLambdaQueryWrapper.eq(Bed::getRoomId, id);
        List<Bed> list = bedService.list(bedLambdaQueryWrapper);
        long bedCount = list.stream().count();
        if(bedCount>0){
            throw new BusinessException("房间内有床位，不能删除");
        }
        // 2.删除房间
        roomService.removeById(id);
        return success();
    }
}
